module executor.loading.server.LoadingExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import hunt.util.Serialize;
import client.MsgServerClient;
import client.ImClient;
import client.LogicServerClient;
import std.array;
import std.conv : to;
import core.stdc.time;
import core.stdc.string;

/**
 * 满足移动端启动加载信息
 */
@TransportServer(gateServerName)
class LoadingExecutor  : AbstractExecutor!(LoadingExecutor){

  @MessageId(MESSAGE.CMD_CONTACTS_REQ)
  void loading(TransportContext ctx, MessageBuffer buffer) {
    auto req = new InitMsg();
    buffer.data.fromProtobuf!InitMsg(req);

    User u = cast(User)ctx.session.getAttribute(SESSIONKEY.USER);
    AuthenticateREQ authorize = cast(AuthenticateREQ)ctx.session.getAttribute(SESSIONKEY.SRC);
    if (u !is null)
    {
      logInfo("session userid : %s",u.id);
      //req.clientId = ImClient.getInstance().idMixSrc( to!ulong(u.id), authorize.src );
      ulong userId = ImClient.getInstance().idMixSrc( to!ulong(u.id), authorize.src );
      //Extend extend;
      //extend.userId = cast(uint)userId;
      //ClientExtend clientExtend = cast(ClientExtend)buffer.extend;
      ServerExtend serverExtend;
      serverExtend.userId = userId;

      ubyte[ServerExtend.sizeof] extend ;
      memcpy(extend.ptr,&serverExtend,ServerExtend.sizeof);
      LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_CONTACTS_REQ,req.toProtobuf.array,extend));
    }else
    {
      logError("no authorized");
      //TODO
    }
  }
}

